package com.xscha.soyue.common.core.config.configure;

import com.xscha.soyue.common.core.property.ThreadPoolProperties;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程池配置
 * @author Vondser
 * @version V2024.1.0
 * 2024/05/17
 */
@AutoConfiguration
@EnableConfigurationProperties(ThreadPoolProperties.class)
public class ThreadPoolAutoConfiguration {

	private final ThreadPoolProperties poolProperties;

	public ThreadPoolAutoConfiguration(ThreadPoolProperties poolProperties) {
		this.poolProperties = poolProperties;
	}

	/**
	 * * 配置线程池
	 * @return {@link ThreadPoolTaskExecutor}
	 */
	@Bean
	public ThreadPoolTaskExecutor TaskExecutor() {
		ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
		// 此方法返回可用处理器的虚拟机的最大数量; 不小于1
		taskExecutor.setBeanName(poolProperties.getBeanName());
		taskExecutor.setThreadGroupName(poolProperties.getThreadGroupName());
		taskExecutor.setCorePoolSize(poolProperties.getCorePoolSize());
		taskExecutor.setMaxPoolSize(poolProperties.getMaxPoolSize());
		taskExecutor.setQueueCapacity(poolProperties.getQueueCapacity());
		taskExecutor.setKeepAliveSeconds(poolProperties.getKeepAliveSeconds());
		taskExecutor.setThreadNamePrefix(poolProperties.getNamePrefix());// 线程名称前缀
		// 线程池对拒绝任务（无线程可用）的处理策略，目前只支持AbortPolicy、CallerRunsPolicy；默认为后者
		taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
		taskExecutor.initialize();
		return taskExecutor;
	}

}
